# EDXLSharp AWS Router Install Script
# Copyright 2014 The MITRE Corporation
# Licensed under the Apache 2.0 Licensed
# Don McGarry

# This script is designed to be run on an AWS instance with the proper IAM role permissions for SQS and Dynamo
# Using roles means that we don't need to store credentials in the script which is a good thing
# This script uses AWS Tools for Powershell which is installed on all AWS Windows AMIs by default
# To use, set the variables below and run the script

# Required user variables:
# The AWS Region to create resources in
$AWSRegion = "us-east-1"
# Your AWS Account Number (this is used to dynamically generate ARNs and Policies)
$AWSAcct = "122017571978"
# The name of the role for your EC2 servers to access the SQS queues
$RoleName = "icnet-dev"
# Read Capacity Units for EDXL Cache
$EDXLRead = 1
# Write  Capacity Units for EDXL Cache
$EDXLWrite = 1
# Read Capacity Units for ValueLists
$VLRead = 1
# Write  Capacity Units for ValueLists
$VLWrite = 1
# Read Capacity Units for Rules
$RulesRead = 1
# Write  Capacity Units for Rules
$RulesWrite = 1


# Constants (You could change these, but why?)
$SQSQueueName = "icnet-incomingedxl"
$EDXLDynamoTableName = "EDXLCache"
$ValueListDynamoTableName = "ValueLists"
$RulesDynamoTableName = "Rules"

# Begin demz code
Import-Module AWSPowerShell
Set-DefaultAWSRegion $AWSRegion
$sqspolicyjson=@"
{
  "Version": "2008-10-17",
  "Id": "/$AWSAcct/icnet-incomingedxl/SQSDefaultPolicy",
  "Statement": [
    {
      "Sid": "Sid8675309311592",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::${AWSAcct}:role/$RoleName"
      },
      "Action": "SQS:*",
      "Resource": "arn:aws:sqs:${AWSRegion}:${AWSAcct}:icnet-incomingedxl"
    }
  ]
}
"@
Write-Host $sqspolicyjson
$sqspolicyjson=$sqspolicyjson -replace "`t|`n|`r",""
$sqspolicyjson=$sqspolicyjson -replace " ",""
Write-Host $sqspolicyjson
$sqsopts = @{"ReceiveMessageWaitTimeSeconds" = "20"; "VisibilityTimeout" = "0"}
$sqsurl=New-SQSQueue -QueueName $SQSQueueName -Attributes $sqsopts
Write-Host $sqsurl
$sqsopts = @{"Policy" = $sqspolicyjson}
Set-SQSQueueAttribute -QueueUrl $sqsurl -Attributes $sqsopts
$tables = Get-DDBTables
Write-Host $tables
$found = 0
foreach ($table in $tables)
{
	if ($table -eq $EDXLDynamoTableName)
	{
		$found = 1
		break
	}
}

if($found -eq 1)
{
	Write-Host "EDXL Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating EDXL DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "DEID" -KeyType "HASH"
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "DistributionTime" -KeyType "RANGE"
	New-DDBTable -TableName $EDXLDynamoTableName -Schema $Schema -ReadCapacity $EDXLRead -WriteCapacity $EDXLWrite
}

$found = 0
foreach ($table in $tables)
{
	if ($table -eq $ValueListDynamoTableName)
	{
		$found = 1
		break
	}
}
if($found -eq 1)
{
	Write-Host "ValueLists Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating ValueLists DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "ValueListURI" -KeyType "HASH"
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "Value" -KeyType "RANGE"
	New-DDBTable -TableName $ValueListDynamoTableName -Schema $Schema -ReadCapacity $VLRead -WriteCapacity $VLWrite
}

$found = 0
foreach ($table in $tables)
{
	if ($table -eq $RulesDynamoTableName)
	{
		$found = 1
		break
	}
}
if($found -eq 1)
{
	Write-Host "Rules Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating Rules DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "FromListURI" -KeyType "HASH"
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "ToListURI" -KeyType "RANGE"
	New-DDBTable -TableName $RulesDynamoTableName -Schema $Schema -ReadCapacity $RulesRead -WriteCapacity $RulesWrite
}